package Graphique;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.security.auth.login.LoginException;
import javax.swing.*;

import Client.Client;

public class Connexion extends JFrame{
	private static final long serialVersionUID = 1L;

	private Client client;
	
	private JLabel intro = new JLabel("Connectes-toi : ");
	private JLabel labelLogin = new JLabel("Login :");
	private JLabel labelMdp = new JLabel("Mot de passe :");
	private JTextField champsLogin = new JTextField();
	private JPasswordField champsMdp = new JPasswordField();
	private JButton connexion = new JButton("Connexion");
	private JButton annuler = new JButton("Annuler"); 
	private ActionListenerConnexion alc = new ActionListenerConnexion();
	
	public Connexion(Client client){
		super("Connexion");
		
		this.setSize(250, 250);
		
		connexion.addActionListener(alc);
		annuler.addActionListener(alc);
		
		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout(2, 2));
		jp.add(labelLogin);
		jp.add(champsLogin);
		jp.add(labelMdp);
		jp.add(champsMdp);
		
		JPanel jp2 = new JPanel();
		jp2.setLayout(new GridLayout(2, 1));
		jp2.add(connexion);
		jp2.add(annuler);
		
		this.add(intro, BorderLayout.NORTH);
		this.add(jp, BorderLayout.CENTER);
		this.add(jp2, BorderLayout.SOUTH);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.pack();
		this.client = client;
	}
	
	private class ActionListenerConnexion implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			Object obj = e.getSource();
			
			if(connexion.equals(obj)){
					try {
						client.connexion(champsLogin.getText(), champsMdp.getText());
						new Connecte(client);
						dispose();
					} catch (RemoteException e1) {
						e1.printStackTrace();
					} catch (LoginException e1) {
						e1.printStackTrace();
					}
			}
			if(annuler.equals(obj)){
					new MainGraphique(client);
					dispose();
			}
			
		}

	}
	

}
